<?php

namespace App\Http\Resources;

use App\Http\Responses\Resource;
use App\Http\Resources\User as UserCollection;
use App\Http\Resources\Chapter as ChapterCollection;

class Post extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {

        // user
        // likes
        return [

            'id' => $this->id,
            'uid' => $this->uid,
            'barNum' => $this->bar_num,
            'bookNum' => $this->book_num,
            'title' => $this->title,
            'content' => $this->content,
            'image' => $this->image,
            'releaseTime' => $this->release_time,
            'articleChapter' => $this->article_chapter,
            'isLike' => $this->isLike,
            'likesCounts' => $this->likedUsers()->count(),
            'commentCount' => $this->comments()->count(),
            'hotAt' => $this->hot_at,
            'chapter' => $this->when($this->chapter,new ChapterCollection($this->chapter)),
            'user' => new UserCollection($this->user),
            'likes' => UserCollection::collection($this->likedUsers()->limit(10)->get())
        ];

    }

    protected function chapter(){

        if (empty($this->chapter)){
            return null;
        }

        $chapter = $this->chapter;

        $book = $chapter->book;

        return [
            'id' => $chapter->id,
            'chapter_num' => $chapter->chapter_num,
            'name' => $chapter->name,
            'create_time' => $chapter->create_time,
            'last_modify_time' => $chapter->last_modify_time,
            'book' => [
                'id' => $book->id,
                'book_num' => $book->book_num,
                'name' => $book->name,
                'labels' => $book->labels,
                'cover' => $book->cover
            ]
        ];



    }
}
